package com.example.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.blog.config.PublisherAware;
import com.example.blog.domain.*;
import com.example.blog.event.publisher.LoggingApplicationEvent;
import com.example.blog.mapper.WebsiteAdministrationMapper;
import com.example.blog.note.LogInfo;
import com.example.blog.note.NoToken;
import com.example.blog.note.Permissions;
import com.example.blog.service.impl.ChangelogServerServiceImpl;
import com.example.blog.service.impl.UuIdServiceImpl;
import com.example.blog.service.impl.WebsiteAdministrationServiceImpl;
import com.example.blog.utils.IpUtil;
import com.example.blog.dto.operationalLogs.OperationLogsDto;
import com.example.blog.dto.webAdministration.UploadWebImageDto;
import com.example.blog.dto.webAdministration.WebsiteAdminDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@RestController
@Transactional
@Validated
@RequestMapping("/websiteAdministration")
public class WebsiteAdministrationController extends BaseController {
    private final WebsiteAdministrationServiceImpl websiteAdministrationService;
    private final WebsiteAdministrationMapper websiteAdministrationMapper;
    private final PublisherAware loggingPublisherAware;

    @Autowired
    public WebsiteAdministrationController(WebsiteAdministrationServiceImpl websiteAdministrationService,
                                           WebsiteAdministrationMapper websiteAdministrationMapper,
                                           PublisherAware loggingPublisherAware) {
        this.websiteAdministrationService = websiteAdministrationService;
        this.websiteAdministrationMapper = websiteAdministrationMapper;
        this.loggingPublisherAware = loggingPublisherAware;
    }

    //获取网站数据
    @NoToken
    @LogInfo("获取网站数据")
    @GetMapping("/getAll")
    public Response getAll() {
        return new Response(Response.SUCCEED_CODE, "获取网站数据", websiteAdministrationService.list());
    }

    //修改网站数据
    @Permissions("/websiteAdministration/update")
    @LogInfo("修改网站数据")
    @PostMapping("/update")
    public Response update(@RequestBody WebsiteAdminDto websiteAdministration) {
        OperationLogsDto logsVo = new OperationLogsDto(
                0, IpUtil.getIpAddress(getHttpInfo()),"修改网站数据",getLogSign(),websiteAdministration.getOptions());
          boolean update = false;
           try {
               LambdaQueryWrapper<WebsiteAdministration> wrapper = new LambdaQueryWrapper<>();
               wrapper.eq(WebsiteAdministration::getOptions, websiteAdministration.getOptions());
               WebsiteAdministration web = new WebsiteAdministration();
               BeanUtils.copyProperties(websiteAdministration,web);
               update = websiteAdministrationService.update(web, wrapper);
               logsVo.setOutcome(1);
               loggingPublisherAware.publisher(new LoggingApplicationEvent(logsVo));
           }catch (Exception e){
               logsVo.setOutcome(0);
               loggingPublisherAware.publisher(new LoggingApplicationEvent(logsVo));
               throw e;
           }
             return new Response(Response.SUCCEED_CODE, "修改成功", String.valueOf(update));
    }


    // 上传单个图片
    @Permissions("/websiteAdministration/uploadImage")
    @LogInfo("上传网站图片")
    @PostMapping("/uploadImage")
    //从表单接收数据
    public Response uploadFile(UploadWebImageDto uploadImageVo) {
        System.out.println(uploadImageVo);
        OperationLogsDto logsDto = new OperationLogsDto(
                0, IpUtil.getIpAddress(getHttpInfo()),"修改网站数据",getLogSign(), null);
        return new Response(Response.SUCCEED_CODE, "修改网站数据", websiteAdministrationService.updateWebImg(uploadImageVo,logsDto));
    }

    //获取网站访问总数
    @NoToken
    @LogInfo("获取网站访问总数")
    @GetMapping("/length")
    public Response webLength() {
        return new Response(Response.SUCCEED_CODE, "获取网站访问总数", websiteAdministrationMapper.webLength());
    }
}
